package com.ai.api.controller;

import com.ai.bo.QuestionLibBo;
import com.ai.enums.YesOrNo;
import com.ai.grace.result.GraceJSONResult;
import com.ai.grace.result.ResponseStatusEnum;
import com.ai.service.QuestionLibService;
import com.ai.utils.JsonUtils;
import com.ai.utils.RedisOperator;
import com.ai.vo.InitQuestionVO;
import com.ai.vo.QuestionLibVo;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("questionLib")
public class QuestionLibController {
    @Autowired
    private QuestionLibService questionLibService;
    @Autowired
    private RedisOperator redisOperator;
    @PostMapping("createOrUpdate")
    public GraceJSONResult createOrUpdate(@RequestBody QuestionLibBo questionLibBo){
        questionLibService.createOrUpdate(questionLibBo);
        return GraceJSONResult.ok();
    }

    @GetMapping("list")
    public GraceJSONResult list(@RequestParam("aiName") String aiName,@RequestParam("question") String question,@RequestParam(defaultValue = "1",name = "page") Integer page,@RequestParam(defaultValue = "10",name = "size") Integer size){
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("aiName",aiName);
        paramsMap.put("question",question);
        PageInfo<QuestionLibVo> questionLibVoPageInfo = questionLibService.QuestionLibList(paramsMap, page, size);
        return GraceJSONResult.ok(questionLibVoPageInfo);
    }

    @PostMapping("hide")
    public GraceJSONResult setHide(@RequestParam("questionLibId") String questionLibId){
        if (StringUtils.isBlank(questionLibId)) return GraceJSONResult.error();
        questionLibService.setDisplayOrNo(questionLibId, YesOrNo.NO.type);
        return GraceJSONResult.ok();
    }
    @PostMapping("show")
    public GraceJSONResult setShow(@RequestParam("questionLibId") String questionLibId){
        if (StringUtils.isBlank(questionLibId)) return GraceJSONResult.error();
        questionLibService.setDisplayOrNo(questionLibId, YesOrNo.YES.type);
        return GraceJSONResult.ok();
    }
    @PostMapping("delete")
    public GraceJSONResult delete(@RequestParam("questionLibId") String questionLibId){
        if (StringUtils.isBlank(questionLibId)) return GraceJSONResult.error();
        questionLibService.delete(questionLibId);
        return GraceJSONResult.ok();
    }

    /**
     * 候选人进入面试时,随机抽取面试题
     * @param candidateId 候选人id
     * @return
     */
    @GetMapping("prepareQuestion")
    public GraceJSONResult prepareQuestion(@RequestParam("candidateId")String candidateId){
//        先判断是不是候选人
        String token = redisOperator.get("USER_TOKEN" + ":" + candidateId);
        if (StringUtils.isBlank(candidateId)||StringUtils.isBlank(token)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_INFO_NOT_EXIST_ERROR);
        }
        // 检查缓存中是否存在已分配的题目
        String  cacheKey = "INTERVIEW_QUESTIONS:" + candidateId;
        String randomQuestionJSON = redisOperator.get(cacheKey);
//        缓存中有题目
        if (!StringUtils.isBlank(randomQuestionJSON))  {
            //        已固化的题目
            List<InitQuestionVO> initQuestionVOS = JsonUtils.jsonToList(randomQuestionJSON, InitQuestionVO.class);
            return GraceJSONResult.ok(initQuestionVOS);  // 直接返回已固化的题目
        }
//        redis中没有已固化的题目
        List<InitQuestionVO> randomQuestion = questionLibService.getRandomQuestion(candidateId, 3);
//        存入
        redisOperator.set(cacheKey, JsonUtils.objectToJson(randomQuestion),1*60*60);
        return GraceJSONResult.ok(randomQuestion);
    }
}
